<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间" prop="createtime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.beginTime" type="date" value-format="yyyy-MM-dd" placeholder="选择开始日期">
            </el-date-picker>
            <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择结束日期">
            </el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="customerid">
        <el-select v-model="queryParams.customerid" placeholder="请选择客户" clearable filterable size="small">
          <el-option
            v-for="dict in customerOptions"
            :key="dict.guid"
            :label="dict.name"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="充值状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择充值状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发起人" prop="operatorid">
        <el-select v-model="queryParams.operatorid" placeholder="请选择发起人" clearable filterable size="small">
          <el-option
            v-for="dict in userOptions"
            :key="dict.guid"
            :label="dict.realname"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="departid">
        <el-select v-model="queryParams.departid" placeholder="请选择发起人" clearable filterable size="small">
          <el-option
            v-for="dict in depts"
            :key="dict.guid"
            :label="dict.name"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:recharge:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:recharge:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:recharge:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:recharge:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="rechargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="code" />
      <el-table-column label="客户名称" align="center" prop="customer" width="250"/>
      <el-table-column label="发起人" align="center" prop="operator" />
      <el-table-column label="部门" align="center" prop="departname" />
      <el-table-column label="充值金额" align="center" prop="amount" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="发起时间" align="center" prop="createtime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值报表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户id" prop="customerid">
          <el-input v-model="form.customerid" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="充值金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="状态:0-已删除 1-待审批 2-已充值" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态:0-已删除 1-待审批 2-已充值">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程id" prop="flowid">
          <el-input v-model="form.flowid" placeholder="请输入流程id" />
        </el-form-item>
        <el-form-item label="操作人id" prop="operatorid">
          <el-input v-model="form.operatorid" placeholder="请输入操作人id" />
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="最后更新时间" prop="lastupdate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.lastupdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户名称" prop="customer">
          <el-input v-model="form.customer" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="部门id" prop="departid">
          <el-input v-model="form.departid" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="部门" prop="departname">
          <el-input v-model="form.departname" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="1-充值 2-退款 3-补货" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入1-充值 2-退款 3-补货" />
        </el-form-item>
        <el-form-item label="退货订单id" prop="orderid">
          <el-input v-model="form.orderid" placeholder="请输入退货订单id" />
        </el-form-item>
        <el-form-item label="退货订单编号" prop="ordercode">
          <el-input v-model="form.ordercode" placeholder="请输入退货订单编号" />
        </el-form-item>
        <el-form-item label="申请金额" prop="applyamount">
          <el-input v-model="form.applyamount" placeholder="请输入申请金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecharge, getRecharge, delRecharge, addRecharge, updateRecharge, exportRecharge } from "@/api/bill/recharge";

import { tuserList, getCustomerList,getDepartmentList} from "@/api/public";

export default {
  name: "Recharge",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值报表表格数据
      rechargeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态:0-已删除 1-待审批 2-已充值字典
      statusOptions: [],
      // 类型字典
      typeOptions: [],
      depts:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerid: null,
        departid:null,
        status: null,
        operatorid: null,
        beginTime: this.getMonthFirDate(),
        endTime: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerid: [
          { required: true, message: "客户id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态:0-已删除 1-待审批 2-已充值不能为空", trigger: "change" }
        ],
        flowid: [
          { required: true, message: "流程id不能为空", trigger: "blur" }
        ],
        operatorid: [
          { required: true, message: "操作人id不能为空", trigger: "blur" }
        ],
        operator: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        lastupdate: [
          { required: true, message: "最后更新时间不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
      },
      customerOptions: [],
      userOptions: [],
    };
  },
  created() {
    this.getList();
    this.tuserList();
    this.getCustomerList();
    this.getDicts("recharge_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("recharge_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getdepts()
  },
  methods: {
    getdepts(){
      getDepartmentList().then(response=>{
        this.depts=response.data
      })
    },
    //用户
    tuserList(){
      tuserList().then(response => {
        this.userOptions = response.data;
      });
    },
    getCustomerList(){
      getCustomerList().then(response => {
        this.customerOptions = response.data;
      });
    },
    /** 查询充值报表列表 */
    getList() {
      this.loading = true;
      listRecharge(this.queryParams).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态:0-已删除 1-待审批 2-已充值字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        customerid: null,
        amount: null,
        status: null,
        flowid: null,
        operatorid: null,
        operator: null,
        createtime: null,
        lastupdate: null,
        customer: null,
        code: null,
        type: null,
        remark: null,
        departid: null,
        departname: null,
        flag: null,
        orderid: null,
        ordercode: null,
        applyamount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值报表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getRecharge(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值报表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateRecharge(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecharge(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecharge(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有充值报表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecharge(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
